package com.ccmusic.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccmusic.entity.ListView;
import com.ccmusic.entity.MusicType;
import com.ccmusic.service.ListMatchingService;
import com.ccmusic.service.ListViewService;
import com.ccmusic.service.MatchingService;
import com.ccmusic.service.PersonalListService;
import com.ccmusic.service.impl.ListMatchingServiceImpl;
import com.ccmusic.service.impl.ListViewServiceImpl;
import com.ccmusic.service.impl.MatchingServiceImpl;
import com.ccmusic.service.impl.PersonalListServiceImpl;
import com.ccmusic.util.DatatablesBean;
import com.google.gson.Gson;

/**
 * Servlet implementation class UserMusicServlet
 */
/**
 * 用户登录后进行播放、新建歌单、收藏等操作所需的控制器
 * 
 * @Author Phantom Assassin
 * @Date 2019年3月28日 下午10:12:30
 */
@WebServlet({ "/UserMusicServlet", "/ums.do" })
public class UserMusicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ListMatchingService lms = new ListMatchingServiceImpl();
	MatchingService ms = new MatchingServiceImpl();
	PersonalListService pls = new PersonalListServiceImpl();
	ListViewService lvs = new ListViewServiceImpl();

	/**
	 * 获取用户默认歌单详情 返回json给前端
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException void
	 */
	protected void getMusicCollection(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 以前自己实现分页，现在用datatables插件完成分页？？
		response.setContentType("application/json");
		PrintWriter pw = response.getWriter();
		String uid = request.getParameter("uid");
		DatatablesBean<MusicType> dtb = ms.getMusicCollectionByUid(uid);
		Gson gson = new Gson();
		pw.print(gson.toJson(dtb));
		pw.close();
	}

	/**
	 * 获取用户所有歌单列表
	 * 
	 * @author 史泽浩
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void getListCollection(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter pw = response.getWriter();
		String uid = request.getParameter("uid");
		DatatablesBean<ListView> dtb = lvs.getLists(uid);
		//System.out.println("servlet的提示消息！！！");
		//System.out.println(dtb);
		Gson gson = new Gson();
		pw.print(gson.toJson(dtb));
		pw.close();
	}

	/**
	 * 获取某个歌单里的详细歌曲列表
	 * 
	 * @author 史泽浩
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void getListDetails(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter pw = response.getWriter();
		String uid = request.getParameter("uid");
		String lid = request.getParameter("lid");
		//System.out.println(uid);
		DatatablesBean<MusicType> dtb = lms.getMusicsFromList(uid, lid);
		//System.out.println(dtb);
		Gson gson = new Gson();
		pw.print(gson.toJson(dtb));
		pw.close();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op = "getlovedsongs";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("getlovedsongs".equals(op)) {
			getMusicCollection(request, response);
		} else if ("getlovedlists".equals(op)) {
			getListCollection(request, response);
		}else if ("getlistdetails".equals(op)) {
			getListDetails(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
